package com.njym.java.security.app.rest;

import com.njym.java.security.app.biz.PhotoContentBiz;
import com.njym.java.security.app.entity.PhotoContent;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("photoContent")
public class PhotoContentController extends BaseController<PhotoContentBiz,PhotoContent> {

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Object lists(@RequestParam String id, @RequestParam String userId) {
        if (!userId.equals("undefined") && !id.equals("undefined")) {
            List<PhotoContent> list = baseBiz.selectListsById(Integer.parseInt(id), Integer.parseInt(userId));
            Map<String, Object> data = new HashMap<>(1);
            data.put("list", list);
            return new ObjectRestResponse<PhotoContent>().rel(true).data(data);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    @ResponseBody
    public Object addPhotos(@RequestParam String id, @RequestParam String pic, @RequestParam String userId) {
        if (!userId.equals("undefined") && !id.equals("undefined")) {
            baseBiz.addPhotos(Integer.parseInt(id), pic, Integer.parseInt(userId));
            return this.lists(id, userId);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

    @RequestMapping(value = "/deletePhotos",method = RequestMethod.GET)
    @ResponseBody
    public Object deletePhotos(@RequestParam String list, @RequestParam String userId) {
        if (!userId.equals("undefined")) {
            String[] arr = list.split(",");
            for (int i = 0; i < arr.length; i++) {
                baseBiz.deletePhotoById(Integer.parseInt(arr[i]), Integer.parseInt(userId));
            }
            return new ObjectRestResponse<>().rel(true);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }
}